<?php

namespace App\Models\Article;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Post
 * @package App\Models\Article
 * @mixin \Eloquent
 */
class Post extends Model
{
    use SoftDeletes;

    public function columnInfo()
    {
        return $this->belongsTo(Column::class, 'c_id');

    }

    public function comments(){
        return $this->hasMany(Comment::class);
    }

    public function setThumbAttribute($thumb)
    {
        if (is_array($thumb)) {
            $this->attributes['thumb'] = json_encode($thumb);
        }
    }

    public function getThumbAttribute($pictures)
    {
        return json_decode($pictures, true);
    }

    public static function getThumb($thumb)
    {
        $data = $thumb;

        $images = [];
        foreach ($data as $k => $v) {

            if (strstr($v, "http://") || strstr($v, "https://")) {
                $images[] = $v;
            } else {
                $images[] = config('admin.upload.host') . $v;
            }


        }
        $count = count($data);
        $type = 0;
        if ($count <= 0) {
            $type = 0;
        } elseif ($count < 3) {
            $type = 1;
        } elseif ($count >= 3) {
            $type = 2;
        }
        return ['type' => $type, 'images' => $images];
    }
}
